﻿using QLBH.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLBH.DATA
{
    public class PhieuNhapData
    {
        private QLBHEntities db = new QLBHEntities();
        public string Error { set; get; }
        public string Result { set; get; }

        public List<PHIEU_NHAP> TimPhieuNhapTheoMa(string ma_phieu_nhap, int tong_so_dong)
        {
            db = new QLBHEntities();
            List<PHIEU_NHAP> list = new List<PHIEU_NHAP>();
            if (ma_phieu_nhap == "")
            {
                list = db.PHIEU_NHAP.ToList().OrderByDescending(p => p.NGAY_NHAP.Date).Skip(tong_so_dong).Take(20).ToList().Select(p => new
                    PHIEU_NHAP()
                    {
                        MA_PHIEU_NHAP = p.MA_PHIEU_NHAP,
                        NGAY_NHAP = p.NGAY_NHAP,
                        KHO_HANG = new KHO_HANG()
                        {
                            MA_KHO_HANG = p.KHO_HANG.MA_KHO_HANG,
                            TEN_KHO_HANG = p.KHO_HANG.TEN_KHO_HANG
                        }
                    }).ToList();
            }
            else
                list = db.PHIEU_NHAP.Where(p => p.MA_PHIEU_NHAP.Contains(ma_phieu_nhap)).ToList().OrderByDescending(p=>p.NGAY_NHAP.Date).Skip(tong_so_dong).Take(200).ToList().Select(p => new
                    PHIEU_NHAP()
                {
                    MA_PHIEU_NHAP = p.MA_PHIEU_NHAP,
                    NGAY_NHAP = p.NGAY_NHAP,
                    KHO_HANG = new KHO_HANG()
                    {
                        MA_KHO_HANG = p.KHO_HANG.MA_KHO_HANG,
                        TEN_KHO_HANG = p.KHO_HANG.TEN_KHO_HANG
                    }
                }).ToList();
            return list;
        }

        public PHIEU_NHAP TimPhieuNhapTheoMa(string ma_phieu_nhap)
        {
            db = new QLBHEntities();
            List<PHIEU_NHAP> list = new List<PHIEU_NHAP>();
            list = db.PHIEU_NHAP.Where(p => p.MA_PHIEU_NHAP.Contains(ma_phieu_nhap)).ToList().OrderBy(p=>p.NGAY_NHAP.Date).ToList();
            return list.FirstOrDefault();
        }

        private string TaoMa()
        {
            string result = "";
            if (db.PHIEU_NHAP.ToList().Count <= 0)
            {
                result = "0001";
            }
            else
            {
                int maxId = 0;

                foreach (var item in db.PHIEU_NHAP.ToList())
                {
                    if (maxId < int.Parse(item.MA_PHIEU_NHAP.Substring(2)))
                    {
                        maxId = int.Parse(item.MA_PHIEU_NHAP.Substring(2));
                    }
                }
                switch (maxId.ToString().Length)
                {
                    case 1:
                        result = "000" + (maxId + 1);
                        break;


                    case 2:
                        result = "00" + (maxId + 1);
                        break;


                    case 3:
                        result = "0" + (maxId + 1);
                        break;


                    case 4:
                        result = "" + (maxId + 1);
                        break;
                    default:
                        break;
                }
            }
            return "PN" + result;
        }

        public bool ThemMoi(PHIEUNHAPPARAM PHIEUNHAP)
        {
            try
            {
                db = new QLBHEntities();
                PHIEU_NHAP pb = new PHIEU_NHAP();
                pb.MA_PHIEU_NHAP = PHIEUNHAP.MA_PHIEU_NHAP;
                pb.MA_NHA_CUNG_CAP = PHIEUNHAP.MA_NHA_CUNG_CAP;
                pb.MA_KHO_HANG = PHIEUNHAP.MA_KHO_HANG;
                pb.MA_NHAN_VIEN = PHIEUNHAP.MA_NHAN_VIEN;
                pb.NGAY_NHAP = DateTime.Now;
                pb.MA_CHUNG_TU_GOC = PHIEUNHAP.MA_CHUNG_TU_GOC;
                pb.TAI_KHOAN_NO = PHIEUNHAP.TAI_KHOAN_NO;
                pb.TAI_KHOAN_CO = PHIEUNHAP.TAI_KHOAN_CO;
                pb.NGUOI_GIAO = PHIEUNHAP.NGUOI_GIAO;
                pb.MO_TA = PHIEUNHAP.MO_TA;

                if (pb.MA_PHIEU_NHAP.Trim().Length <= 0)
                {
                    pb.MA_PHIEU_NHAP = TaoMa();
                }
                else if (db.PHIEU_NHAP.Any(p => p.MA_PHIEU_NHAP == pb.MA_PHIEU_NHAP))
                {
                    Error = "Mã Phiếu nhập đã tồn tại";
                    return false;
                }

                foreach (var item in PHIEUNHAP.CHITIETNHAP)
                {
                    CHI_TIET_NHAP CT = new CHI_TIET_NHAP();
                    CT.GIA_NHAP = item.GIA_NHAP;
                    CT.HAN_SU_DUNG = item.HAN_SU_DUNG;
                    CT.MA_PHIEU_NHAP = PHIEUNHAP.MA_PHIEU_NHAP;
                    CT.MA_SAN_PHAM = item.MA_SAN_PHAM;
                    CT.SO_LUONG = item.SO_LUONG;
                    pb.CHI_TIET_NHAP.Add(CT);

                    var tonkho = db.TON_KHO.ToList().Where(p => p.MA_KHO_HANG == PHIEUNHAP.MA_KHO_HANG && p.NGAY_TON_KHO.Date == DateTime.Now.Date && p.MA_SAN_PHAM == item.MA_SAN_PHAM).FirstOrDefault();
                    if (tonkho != null)
                    {
                        tonkho.NHAP_TRONG_KY += CT.SO_LUONG;
                        tonkho.TON_CUOI_KY += CT.SO_LUONG;
                        tonkho.TONG_GT_NHAP += CT.SO_LUONG * CT.GIA_NHAP;
                        tonkho.DON_GIA_BINH_QUAN = tonkho.TONG_GT_NHAP / tonkho.TON_CUOI_KY;
                    }
                    else
                    {
                        var toncu = db.TON_KHO.ToList().Where(p => p.MA_KHO_HANG == PHIEUNHAP.MA_KHO_HANG&& p.MA_SAN_PHAM == item.MA_SAN_PHAM).OrderByDescending(p => p.NGAY_TON_KHO.Date).FirstOrDefault();

                        TON_KHO T = new TON_KHO();
                        T.MA_KHO_HANG = PHIEUNHAP.MA_KHO_HANG;
                        T.MA_SAN_PHAM = item.MA_SAN_PHAM;
                        T.NGAY_TON_KHO = DateTime.Now.Date;
                        T.TON_DAU_KY = 0;
                        T.NHAP_TRONG_KY = item.SO_LUONG;
                        T.XUAT_TRONG_KY = 0;
                        T.TON_CUOI_KY = item.SO_LUONG;
                        T.BAO_HANH = 0;
                        T.TONG_GT_NHAP = CT.SO_LUONG * CT.GIA_NHAP;
                        T.DON_GIA_BINH_QUAN = CT.GIA_NHAP;

                        if (toncu != null)
                        {
                            T.TON_DAU_KY = toncu.TON_CUOI_KY;
                            T.TON_CUOI_KY = T.TON_DAU_KY + T.NHAP_TRONG_KY - T.XUAT_TRONG_KY;
                            T.TONG_GT_NHAP += toncu.TONG_GT_NHAP;
                            T.DON_GIA_BINH_QUAN = Math.Round(T.TONG_GT_NHAP / T.TON_CUOI_KY);
                        }

                        db.TON_KHO.Add(T);
                    }
                }
                Result = pb.MA_PHIEU_NHAP;
                db.PHIEU_NHAP.Add(pb);
                db.SaveChanges();
                
                return true;
            }
            catch (Exception ex)
            {
                Error = ex.Message + " " + ex.InnerException.Message;
                return false;
            }
        }

        
    }
}